package no.hig.imt4721.preprocessing;

import java.io.*;
import java.util.*;

import no.hig.imt4721.model.Repetition;
import no.hig.imt4721.model.Session;
import no.hig.imt4721.model.Subject;

public class Preprocessor
{	
	private static int NUMBER_OF_SESSIONS = 8;
	private static int NUMBER_OF_REPETITIONS = 50;
	private static int NUMBER_OF_SUBJECTS_INCLUDING_EMPTY_SUBJECTS = 56;
	private static int[] ID_OF_EMPTY_SUBJECTS = {4, 6, 10, 18, 39};
	
	public List<Subject> preprocessAll(String filename) throws Exception
	{	
		//	Make data-structure for information
		List<Subject> listOfSubjects = createDataStructure();
		
		//	Fill data-structure with information
		readDataFromFileAll(listOfSubjects, filename);
		
		//	Remove empty subjects from the subject-list
		listOfSubjects = removeEmptySubjects(listOfSubjects);
		
		return listOfSubjects;
	}

	private void readDataFromFileAll(List<Subject> listOfSubjects, String filename) throws IOException 
	{
		int numberOfColumns = 31;
		
		FileInputStream fileInputStream = new FileInputStream(filename);
		DataInputStream dataInputStream = new DataInputStream(fileInputStream);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
		
		String lineInFile;
		bufferedReader.readLine();	// Remove first line in file
		
		while ((lineInFile = bufferedReader.readLine()) != null)
		{
			String[] split = lineInFile.split(",");
		
			String subjectName = split[0];
			
			int subject = Integer.parseInt(split[0].substring(2, 4)) - 1;
			int session = Integer.parseInt(split[1]);
			int repetition = Integer.parseInt(split[2]);
		
			String[] data = new String[numberOfColumns];
			System.arraycopy(split, 3, data, 0, numberOfColumns);

			for (int element = 0; element < numberOfColumns; element++)
			{
				if (element == 0)
					listOfSubjects.get(subject-1).setName(subjectName);
				
				listOfSubjects.get(subject-1).getSessions().get(session-1).getRepetitions().get(repetition-1).addRowElement(Double.parseDouble(data[element]));
			}
		}
	}

	
	
	
	public List<Subject> preprocessDurations(String filename) throws Exception
	{	
		//	Make data-structure for information
		List<Subject> listOfSubjects = createDataStructure();
		
		//	Fill data-structure with information
		readDataFromFileDurations(listOfSubjects, filename);
		
		//	Remove empty subjects from the subject-list
		listOfSubjects = removeEmptySubjects(listOfSubjects);
		
		return listOfSubjects;
	}

	private void readDataFromFileDurations(List<Subject> listOfSubjects, String filename) throws IOException 
	{
		int numberOfColumns = 31;
		
		FileInputStream fileInputStream = new FileInputStream(filename);
		DataInputStream dataInputStream = new DataInputStream(fileInputStream);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
		
		String lineInFile;
		bufferedReader.readLine();	// Remove first line in file
		
		while ((lineInFile = bufferedReader.readLine()) != null)
		{
			String[] split = lineInFile.split(",");
		
			String subjectName = split[0];
			
			int subject = Integer.parseInt(split[0].substring(2, 4)) - 1;
			int session = Integer.parseInt(split[1]);
			int repetition = Integer.parseInt(split[2]);
		
			String[] data = new String[numberOfColumns];
			System.arraycopy(split, 3, data, 0, numberOfColumns);

			for (int element = 0; element < numberOfColumns; element = element + 3 )
			{
				if (element == 0)
					listOfSubjects.get(subject-1).setName(subjectName);
				
				listOfSubjects.get(subject-1).getSessions().get(session-1).getRepetitions().get(repetition-1).addRowElement(Double.parseDouble(data[element]));
			}
		}
	}
	
	
	
	
	
	
	
	public List<Subject> preprocessLatencies(String filename) throws Exception
	{	
		//	Make data-structure for information
		List<Subject> listOfSubjects = createDataStructure();
		
		//	Fill data-structure with information
		readDataFromFileLatencies(listOfSubjects, filename);
		
		//	Remove empty subjects from the subject-list
		listOfSubjects = removeEmptySubjects(listOfSubjects);
		
		return listOfSubjects;
	}

	private void readDataFromFileLatencies(List<Subject> listOfSubjects, String filename) throws IOException 
	{
		int numberOfColumns = 31;
		
		FileInputStream fileInputStream = new FileInputStream(filename);
		DataInputStream dataInputStream = new DataInputStream(fileInputStream);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
		
		String lineInFile;
		bufferedReader.readLine();	// Remove first line in file
		
		while ((lineInFile = bufferedReader.readLine()) != null)
		{
			String[] split = lineInFile.split(",");
		
			String subjectName = split[0];
			
			int subject = Integer.parseInt(split[0].substring(2, 4)) - 1;
			int session = Integer.parseInt(split[1]);
			int repetition = Integer.parseInt(split[2]);
		
			String[] data = new String[numberOfColumns];
			System.arraycopy(split, 3, data, 0, numberOfColumns);

			for (int element = 2; element < numberOfColumns; element = element + 3 )
			{
				if (element == 0)
					listOfSubjects.get(subject-1).setName(subjectName);
				
				listOfSubjects.get(subject-1).getSessions().get(session-1).getRepetitions().get(repetition-1).addRowElement(Double.parseDouble(data[element]));
			}
		}
	}
	
	
	
	
	private List<Subject> createDataStructure() 
	{
		List<Subject> listOfSubjects = new ArrayList<Subject>();
		
		for (int i = 0; i < NUMBER_OF_SUBJECTS_INCLUDING_EMPTY_SUBJECTS; i++)	//	Create subjects
			listOfSubjects.add(new Subject());	
		
		for (Subject subject : listOfSubjects) 
			for (int k = 0; k < NUMBER_OF_SESSIONS; k++)	//	Create sessions for each subject
				subject.addSession(new Session());
		
		for (Subject subject : listOfSubjects) 
			for (Session session : subject.getSessions())
				for (int m = 0; m < NUMBER_OF_REPETITIONS; m++)	//	Create repetitions for each session that is inside of each subject
					session.addRepetition(new Repetition());
		
		return listOfSubjects;
	}
	
	private static List<Subject> removeEmptySubjects(List<Subject> subjects) 
	{
		for (int emptySubjectID = 0; emptySubjectID < ID_OF_EMPTY_SUBJECTS.length; emptySubjectID++)
			subjects.remove(ID_OF_EMPTY_SUBJECTS[emptySubjectID]);
		
		return subjects;
	}
}
